{extend name='admin@main'}

{block name="content"}
<script>
layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  
  //普通图片上传
  var uploadInst = upload.render({
    elem: '#test1'
    ,url: '/upload/'
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#demo1').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
  
  //多图片上传
  upload.render({
    elem: '#test2'
    ,url: '/upload/'
    ,multiple: true
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
      });
    }
    ,done: function(res){
      //上传完毕
    }
  });
  
  //指定允许上传的文件类型
  upload.render({
    elem: '#test3'
    ,url: '/upload/'
    ,accept: 'file' //普通文件
    ,done: function(res){
      console.log(res)
    }
  });
  upload.render({ //允许上传的文件后缀
    elem: '#test4'
    ,url: '/upload/'
    ,accept: 'file' //普通文件
    ,exts: 'zip|rar|7z' //只允许上传压缩文件
    ,done: function(res){
      console.log(res)
    }
  });
  upload.render({
    elem: '#test5'
    ,url: '/upload/'
    ,accept: 'video' //视频
    ,done: function(res){
      console.log(res)
    }
  });
  upload.render({
    elem: '#test6'
    ,url: '/upload/'
    ,accept: 'audio' //音频
    ,done: function(res){
      console.log(res)
    }
  });
  
  //设定文件大小限制
  upload.render({
    elem: '#test7'
    ,url: '/upload/'
    ,size: 60 //限制文件大小，单位 KB
    ,done: function(res){
      console.log(res)
    }
  });
  
  //同时绑定多个元素，并将属性设定在元素上
  upload.render({
    elem: '.demoMore'
    ,before: function(){
      layer.tips('接口地址：'+ this.url, this.item, {tips: 1});
    }
    ,done: function(res, index, upload){
      var item = this.item;
      console.log(item); //获取当前触发上传的元素，layui 2.1.0 新增
    }
  })
  
  //选完文件后不自动上传
  upload.render({
    elem: '#test8'
    ,url: '/upload/'
    ,auto: false
    //,multiple: true
    ,bindAction: '#test9'
    ,done: function(res){
      console.log(res)
    }
  });
  
  //拖拽上传
  upload.render({
    elem: '#test10'
    ,url: '/upload/'
    ,done: function(res){
      console.log(res)
    }
  });
  
	//多文件列表示例
	var demoListView = $('#demoList'),
	uploadListIns = upload.render({
		elem: '#testList',
		url: '{:url("upload")}',
		accept: 'file',
		multiple: true,
		auto: false,
		bindAction: '#testListAction',
		choose: function(obj) {
			var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
				//console.log(obj);
				//读取本地文件
				obj.preview(function(index, file, result) {
			
				//console.log(index);
				
				var tr = $(['<tr id="upload-' + index + '">', '<td>' + file.name + '</td>', '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join(''));

				//单个重传
				tr.find('.demo-reload').on('click',
				function() {
					obj.upload(index, file);
				});

				//删除
				tr.find('.demo-delete').on('click',
				function() {
					delete files[index]; //删除对应的文件
					tr.remove();
					uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
				});

				demoListView.append(tr);
			});
		},
		done: function(res, index, upload) {
			//console.log(index);
			
			//var files = this.files = upload.pushFile(); //将每次选择的文件追加到文件队列
			
			//console.log(upload);
			if (res.code == 0) { //上传成功
				var tr = demoListView.find('tr#upload-' + index),
				tds = tr.children();
				var str ='<input type="hidden" name="fileurl[]" value="'+res.url+'"><input type="hidden" name="user_id[]" value="'+res.user_id+'">';
				str +='<input type="hidden" name="original_name[]" value="'+res.original_name+'"><input type="hidden" name="fileext[]" value="'+res.extension+'">';
				str +='<input type="hidden" name="filesize[]" value="'+res.size+'"><input type="hidden" name="filename[]" value="'+res.save_name+'">';
				str +='<input type="hidden" name="md5code[]" value="'+res.md5+'"><input type="hidden" name="sha1code[]" value="'+res.sha1+'">';
				
				tds.eq(2).html('<span style="color: #5FB878;">上传成功'+str+'</span>');
				tds.eq(3).html('<a href="'+res.url+'" target="new">查看</a>&nbsp;<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'); //清空操作
				
				//删除
				tr.find('.demo-delete').on('click',
				function() {
					//delete this.files[index]; //删除对应的文件
					tr.remove();
					uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
				});
				
				return delete this.files[index]; //删除文件队列已经上传成功的文件
			}
			this.error(index, upload);
		},
		error: function(index, upload) {
			var tr = demoListView.find('tr#upload-' + index),
			tds = tr.children();
			tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
			tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
		}
	});
  
  //绑定原始文件域
  upload.render({
    elem: '#test20'
    ,url: '/upload/'
    ,done: function(res){
      console.log(res)
    }
  });
  
});
</script>





<form class="layui-form layui-card" action="{:request()->url()}" data-auto="true" method="post" autocomplete="off">

	<div class="layui-card-body think-box-shadow padding-left-40">
    <!--<div class="layui-card-body">-->
        <div class="layui-form-item">
            <label class="layui-form-label label-required">设备名称</label>
            <div class="layui-input-block">
                <input name="devicename" value='{$vo.devicename|default=""}' required placeholder="设备名称" class="layui-input">
            </div>
        </div>
		

        <div class="layui-form-item nowrap">
            <label class="layui-form-label ">序列号</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
                <input name="snnum" value='{$vo.snnum|default="-"}'  placeholder="序列号" class="layui-input">
            </div>
			
            <label class="layui-form-label">厂商型号</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
                <input type="text" name="brand" value='{$vo.brand|default="-"}'  placeholder="厂商型号" class="layui-input" >
            </div>
			
            <label class="layui-form-label">数量</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
                <input type="text" name="numbers" value='{$vo.numbers|default="1"}'  placeholder="厂商型号" class="layui-input" >
            </div>			
			
			
        </div>
		
		
        <div class="layui-form-item nowrap">
            <label class="layui-form-label label-required">购买时间</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
                <input type="text" data-date-input="date" name="buytime" value='{$vo.buytime|format_datetime='Y-m-d'|default=""}' required placeholder="购买时间" class="layui-input" >
            </div>

			
			
			
            <label class="layui-form-label ">资产类型</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
			  <select name="types" lay-verify="required">
			  {notempty name='vo.id'}<option selected value="{$vo.types}">{$vo.types}</option>{/notempty}
				<option value=""></option>
				
				<option value="台式电脑">台式电脑</option>
				<option value="笔记本">笔记本</option>
				<option value="打印机">打印机</option>
				<option value="多功能一体机">多功能一体机</option>
				<option value="投影仪">投影仪</option>
				<option value="数码相机">数码相机</option>
				<option value="录像机">录像机</option>
				<option value="音响功放">音响功放</option>
				<option value="移动硬盘">移动硬盘</option>
				<option value="办公耗材">办公耗材</option>
				<option value="其他">其他</option>

			  </select>
            </div>
			
            <label class="layui-form-label ">当前状态</label>
            <div class="layui-input-inline margin-right-0" style="width:150px">
			  <select name="status" lay-verify="required">
				
				{notempty name='vo.id'}<option selected value="{$vo.status}">{$vo.status}</option>{/notempty}
				<option value=""></option>

				<option value="完好">完好</option>
				<option value="损坏">损坏</option>
				<option value="待修">待修</option>
				<option value="修理中">修理中</option>
				<option value="报废">报废</option>
				<option value="遗失">遗失</option>

			  </select>
            </div>
			
        </div>
		

        <div class="layui-form-item">
            <label class="layui-form-label ">所在位置</label>
            <div class="layui-input-block">
                <input name="localtion" value='{$vo.localtion|default=""}' placeholder="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label ">备   注</label>
            <div class="layui-input-block">
                <input name="remark" value='{$vo.remark|default=""}' placeholder="" class="layui-input">
            </div>
        </div>


		<script src="__ROOT__/static/plugs/select/xm-select.js"></script>
		<script>
		var username = xmSelect.render({
			el: '#username', 
			name: 'username',
			radio: false,
			layVerify: 'required',
			layVerType: 'tips',
			theme: {
				color: '#f37b1d',
			},
			paging: true,
			pageSize: 10,
			filterable: true,
			data: [
				{$str}
			]
		})
		
		
		
		{if isset($vo) }
		username.setValue([ {$username} ])
		{/if}
		</script>

		
        <div class="layui-form-item">
            <label class="layui-form-label label-required">使用人</label>
            <div class="layui-input-block">
				<div id="username" class="xm-select-demo"></div>

            </div>
        </div>
		

		
        <div class="layui-form-item">
            <label class="layui-form-label ">文件列表</label>
            <div class="layui-input-block">
                <div class="layui-textarea padding-bottom-10" style="min-height:auto">
					<div class="layui-upload">
					  {if auth("edit") && request()->action() =='edit'  && $vo['docauthor']==session('admin_user')['nickname']  }
					  <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="testList">选择多文件</button> &nbsp;&nbsp;
					  <button type="button" class="layui-btn layui-btn-sm" id="testListAction">开始上传</button>
					  {/if}
					  
					  {if auth("add") && request()->action() =='add'  }
					  <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="testList">选择多文件</button> &nbsp;&nbsp;
					  <button type="button" class="layui-btn layui-btn-sm" id="testListAction">开始上传</button>
					  {/if}				  
					  
					  
					  <button type="button" class="layui-btn layui-btn-warm layui-btn-sm" id="zipfile">批量下载</button>
					  
					  <div class="layui-upload-list">
						<table class="layui-table">
						  <thead>
							<tr><th>文件名</th>
							<th>大小</th>
							<th>状态</th>
							<th>操作</th>
						  </tr></thead>
						  <tbody id="demoList">
						  {foreach $files as $key=>$vo}
						  <tr id="upload-{$vo.id}"><td>{$vo.original_name}</td><td>{$vo.filesize|format_size}</td><td></td><td> 
							
							{php}
							$pdffile = ROOTPATH.str_replace($vo['fileext'],"pdf",$vo['fileurl']);
							$pdffile =str_replace("\\","/",$pdffile );
							//echo $pdffile;
							{/php}
							{if file_exists($pdffile)}
						  	<a target="new" href="{:str_replace($vo['fileext'],"pdf",$vo['fileurl'])}">查看</a>
						  	{else}
						  	<a target="new" href="{$vo.fileurl}">下载</a>
						  	{/if}

							&nbsp;&nbsp;
						  	{if $vo.fileext =='doc' || $vo.fileext =='docx' || $vo.fileext =='wps' || $vo.fileext =='xls' || $vo.fileext =='xlsx'}
						  	{if !file_exists($pdffile)}
							<a class="layui-btn layui-btn-xs" href='javascript:void(0);' onclick="changepdf('{$vo.fileurl}' , '{$vo.fileext}');" data-confirm="确定要转换pdf吗？">转换pdf</button>
							{/if}
							{/if}
						  </td>  <tr>
						  {/foreach}
						  </tbody>
						</table>
					  </div>
					  
					</div> 


                </div>
            </div>
        </div>
		

		
		
		

    </div>

    <div class="hr-line-dashed"></div>
    {notempty name='id'}<input type='hidden' value='{$id}' name='id' id="id">{/notempty}
	<input type='hidden' value='{$docauthor}' name='docauthor'>
    <div class="layui-form-item text-center">
		{if  $docauthor ==session('admin_user')['nickname']  }
        <button class="layui-btn" type='submit'>编辑</button>
		{/if}
		{if request()->action() =='add' }
		<button class="layui-btn" type='submit'>保存</button>
		{/if}
		<button class="layui-btn layui-btn-danger" onclick="hsitoryBack()" type="button">关闭</button>
    </div>

    <script>

	window.form.render();
	
	function hsitoryBack(){
		$.msg.confirm('确定要关闭吗？', function (index) {
			history.back(), $.msg.close(index);
		});
	};
	
	

	</script>

</form>

<script>
	$("#zipfile").click(function(){
	  //alert('aa')
	  var id =$("#id").val();
	  if(id ==undefined || id ==null){
	  	layer.msg('文件列表为空!', { icon: 2 });
	  	return false;
	  }
      $.ajax({
            url: "{:url('zipfile')}?id="+id ,
            //data: { url: url},
            dataType: "json",
            type: "get", 
            async: false, //同步异步
			beforeSend:function () {
				
				//this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
			},            
            success: function (data) {
            	//console.log(data);
            	
                //if (data.indexOf("成功")>0) {
                if (data['code'] ==200) {
                	//alert('转换成功');
                	layer.msg('压缩成功，准备下载！', { icon: 1 });
                	downloadFile(data['down']);
                    //window.parent.location.reload();
                    //window.parent.close();
                } else {
                    layer.msg(data['msg'], { icon: 2 });
                    return false;
                }
            }
        });

	});

	function downloadFile(url){
	      var form=$("<form>");
	      form.attr("style","display:none");
	      form.attr("target","");
	      form.attr("method","get"); 
	      form.attr("action",url);
	      $("body").append(form);
	      form.submit();//表单提交}
	}

	function download(url){
	    try {
	          var elemIF = document.createElement("iframe");
	          elemIF.src = url;
	          elemIF.style.display = "none";
	          document.body.appendChild(elemIF);
	        } catch (e) {
	          alert("下载异常！");
	        }
	}


    function changepdf(url , ext) {
        $.ajax({
            url: "{:url('changepdf')}?url="+url+'&ext='+ext,
            //data: { url: url},
            dataType: "json",
            type: "post", 
            async: false, //同步异步
			beforeSend:function () {
				
				//this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
			},            
            success: function (data) {
            	console.log(data);
            	
                //if (data.indexOf("成功")>0) {
                if (data['code'] ==200) {
                	alert('转换成功');
                    layer.msg('转换成功！', { icon: 1 });
                    window.parent.location.reload();
                    window.parent.close();
                } else {
                    layer.msg('转换失败！', { icon: 2 });
                }
            }
        });
    }


</script>


{/block}